package cn.jsl.pet.controller;

import cn.jsl.basic.util.AjaxResult;
import cn.jsl.basic.util.PageList;
import cn.jsl.pet.domain.Pet;
import cn.jsl.pet.query.PetQuery;
import cn.jsl.pet.service.IPetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.save(pet);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                petService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.queryPage(query);
    }

    /**
    * 批量删除, (restful风格接口中,批量相关的都要用这个注解: @PatchMapping("/{id}"))
    * @param ids
    * @return
    */
    @PatchMapping // 这里地址不带值
    //Delete http://localhost:8080/department/1
    @ApiOperation(value = "批量删除数据" )
    public AjaxResult batchRemove(@RequestBody List<Long> ids){ // 前端传过来的List<ids>
        // 包起来,快捷键: ctr + alt + T
        try {
            petService.patchDelete(ids); // ids是前端传过来的ids数组,包含了批量删除的部门对象的id.
            return AjaxResult.me(); // 默认成功信息
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试.."+e.getMessage())
                    .setSuccess(false); // 链式编程
        }
    }

    /**
     * 宠物上架
     */
    @PatchMapping("/onsale")
    //前端传递的是数组 后端可以用数组也可以用集合
    public AjaxResult onsale(@RequestBody Long[] ids, HttpServletRequest request){
        try {
            petService.onsale(ids,request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"宠物上架失败!");
        }
    }

    /**
     * 宠物下架
     */
    @PatchMapping("/offsale")
    //前端传递的是数组 后端可以用数组也可以用集合
    public AjaxResult offsale(@RequestBody Long[] ids){
        try {
            petService.offsale(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"宠物下架失败!");
        }
    }

}
